package com.plian.system.controller.sys.orgzation;

import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.entity.sys.Org;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.sys.orgzation.OrgzationService;
import com.plian.system.vo.sys.OrgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单位树
 */
@RestController
@RequestMapping(value = "api/org")
public class OrgzationController extends BaseController {
    @Autowired
    OrgzationService orgzationService;
    @Autowired
    IOrgService orgService;

    @PostMapping(value = "/list")
    public ListResult<PageData> list(@RequestBody PageData pd) {
        return orgzationService.orgZtree(pd);
    }

    @PostMapping(value = "/save")
    @LogOperation("组织-保存")
    public ObjResult save(@RequestBody PageData pd) {
        return orgzationService.save(pd);
    }

    @PostMapping(value = "/edit")
    @LogOperation("组织-编辑")
    public ObjResult edit(@RequestBody PageData pd) {
        return orgzationService.edit(pd);
    }

    @PostMapping(value = "/del_ids")
    @LogOperation("组织-删除")
    public ObjResult del_ids(@RequestBody PageData pd) {
        return orgzationService.del_ids((List<String>) pd.get("ids"));
    }

    @PostMapping(value = "/findid")
    public ObjResult findid(@RequestBody PageData pd) {
        return orgzationService.findbyid(pd);
    }

    @PostMapping(value = "/adjustment")
    @LogOperation("组织-批量调整")
    public ObjResult adjustment(@RequestBody PageData pd) {
        return orgzationService.adjustment(pd);
    }

    @PostMapping(value = "/listAll")
    public ObjResult listAll() {
        return orgzationService.listAll();
    }
    @PostMapping(value = "/listAllByBasics")
    public ObjResult listAllByBasics() {
        return orgzationService.listAllByBasics();
    }

    @PostMapping(value = "/getSubordinate")
    public ListResult<PageData> getSubordinate() {
        return orgzationService.getSubordinateNoEmptyChildren();
    }
    @PostMapping(value = "/getSubordinateByBasics")
    public ListResult<PageData> getSubordinateByBasics() {
        return orgzationService.getSubordinateByBasics();
    }

    @PostMapping(value = "/getChlid")
    public ListResult<PageData> getChlid(@RequestBody PageData pd) {
        return orgzationService.getChlid(pd);
    }

    @PostMapping(value = "/getChildren")
    public ListResult<PageData> getChildren(@RequestBody PageData pd) {
        return orgzationService.getChildren(pd);
    }

    @PostMapping("/getOneUnit")
    public ListResult getOneUnit() {
        return orgzationService.getOneUnit(false);
    }

    @PostMapping("/getOneUnitByToken")
    public ListResult getOneUnitByToken() {
        return orgzationService.getOneUnit(true);
    }

    @PostMapping(value = "/getGroup")
    public ListResult<PageData> getGroup() {
        return orgzationService.getGroup();
    }

    @PostMapping(value = "/getGroupbyId")
    public ObjResult getGroupbyId(@RequestBody PageData pd) {
        return orgzationService.getGroupbyId(pd);
    }

    @PostMapping(value = "/getGroupAndState")
    public ListResult getGroupAndState() {
        return orgzationService.getGroupAndState();
    }

    @PostMapping(value = "/getSubordinatePassFictitious")
    public ListResult getSubordinatePassFictitious() {
        return orgzationService.getSubordinatePassFictitious();
    }

    @PostMapping(value = "/getPassFictitious")
    public ListResult getPassFictitious() {
        return orgzationService.getPassFictitious();
    }

    @PostMapping(value = "/isHaveChild")
    public ObjResult isHaveChild() {
        return orgzationService.isHaveChild();
    }

    /**
     * 获取下级及本级单位树 (根据 sys_org_relation表特殊处理)
     *
     * @return
     */
    @PostMapping(value = "/getRelationSubordinate")
    public ListResult<PageData> getRelationSubordinate() {
        return orgzationService.getRelationSubordinate();
    }

    /**
     * 获取十三家集团企业
     *
     * @return
     */
    @PostMapping(value = "/findThirteen")
    public ListResult<PageData> findThirteen() {
        return orgzationService.findThirteen();
    }

    /**
     * 获取十三家集团企业-列表型
     *
     * @return
     */
    @PostMapping(value = "/findThirteenOrg")
    public ListResult<Org> findThirteenOrg() {
        return orgService.directlyUnderUnitNoChild(true);
    }

//    /** TODO DEL
//     * 获取十三家集团企业(包括国资委)-列表型
//     *
//     * @return
//     */
//    @PostMapping(value = "/thirteenOrgAddGzw")
//    public ListResult<Org> findThirteenOrgAddGZW() {
//        return orgService.findThirteenOrgAddGZW();
//    }

    /**
     * 后台获取13家企业信息
     *
     * @param
     * @return com.plian.system.model.ListResult<com.plian.system.entity.sys.Org>
     **/
    @PostMapping(value = "/findThirteenOrgOfBackend")
    public ListResult<Org> findThirteenOrgOfBackend() {
        return orgService.findThirteenOrgOfBackend();
    }


    @PostMapping(value = "/findThirteenList")
    public ListResult<PageData> findThirteenList() {
        return orgzationService.findThirteenOrg();
    }

    @PostMapping(value = "/saveNoOrgcode")
    @LogOperation("组织-保存子节点并自动生成编码")
    public ObjResult saveNoOrgcode(@RequestBody PageData pd) {
        return orgzationService.saveNoOrgcode(pd);
    }

    @PostMapping(value = "/getSubordinatePassOwns")
    public ListResult<PageData> getSubordinatePassOwns(@RequestBody PageData pd) {
        return orgzationService.getSubordinatePassOwns(pd);
    }

    @GetMapping(value = "/fillingOrg/{formType}")
    public ListResult<String> getFillingOrg(@PathVariable Integer formType) {
        return orgzationService.getFillingOrg(formType);
    }

    /**
     * 生成简单的产权数据
     *
     * @return
     */
    @PostMapping(value = "/createProperty")
    @LogOperation("生成产权数据")
    public ObjResult createProperty(@RequestBody PageData pd) {
        return orgzationService.createProperty(pd.getString("id"));
    }

    /**
     * 删除简单的产权数据
     *
     * @return
     */
    @PostMapping(value = "/deleteProperty")
    @LogOperation("删除产权数据")
    public ObjResult deleteProperty(@RequestBody PageData pd) {
        return orgzationService.deleteProperty(pd.getString("id"));
    }

    @GetMapping(value = "/getGroupTree")
    public R<PageData> getGroupTree() {
        return orgzationService.getGroupTree();
    }

    @GetMapping(value = "/main-indicators-org-tree")
    public R<List<OrgVO>> findMainIndicatorsOrgTree() {
        return R.data(orgService.findMainIndicatorsOrgTree());
    }

    @PostMapping(value = "/superiorUnit")
    public R<Org> getSuperiorUnit(@RequestBody PageData pd) {
        return R.data(orgService.getSuperiorUnit(pd.getString("id")));
    }
}
